#-*- coding:utf-8 -*-
import sys
import os
import shutil
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from Widgets.main_table_widget import MainTableWidget
from Widgets.rename import Rename
from Widgets.file_list_widget_model import FileListWidget
from Widgets.project_submit_widget import ProjectListWidget
from Widgets.radio_button_model import RadioWidget
from Widgets.department_widget import DepartmentWidget
from Widgets.epi_widget import EpiWidget
from Widgets.baidupan import YunPan
from Widgets.set__project_path import SetProjectPath
from Widgets.shot_info import ShotInfo
from Widgets.time_info_widget import TimeInfo
from Widgets.delete_warn import DeleteWarn
from Widgets.opinion_widget import Opinion
from Widgets.search_widget import Search
from Widgets.palette_color import showColor
from Windows.CreateProject import CreateProject
from Windows.CreateDepartment import CreateDepartment
from Windows.CreateShot import CreateShot

class CreateShotTypeChoose(QWidget):
    create_type_Signal=Signal(int)
    def __init__(self):
        super(CreateShotTypeChoose, self).__init__()
        palette=QPalette()
        palette.setColor(palette.Background,QColor(75,75,75))
        self.setPalette(palette)
        self.main_layout=QHBoxLayout()
        self.text_btn=QPushButton('通过文本创建')
        self.text_btn.clicked.connect(self._createByText)
        self.video_btn=QPushButton('通过视频窗口')
        self.video_btn.clicked.connect(self._creatByVideo)
        self.main_layout.addWidget(self.text_btn)
        self.main_layout.addWidget(self.video_btn)
        self.setLayout(self.main_layout)

    def _createByText(self):
        """
        通过文本创建
        """
        self.create_type_Signal.emit(0)

    def _creatByVideo(self):
        """
        通过视频创建
        """
        self.create_type_Signal.emit(1)

class DepartmentRefresh(QThread):

    def __init__(self):
        super(DepartmentRefresh,self).__init__()

    def run(self):
        self.department_radio_widget.setRadioBtnList(self.deparmentList)


class MainWindow(QMainWindow):

    def __init__(self,username,project_info_path):
        super(MainWindow,self).__init__()

        palette=QPalette()
        palette.setColor(palette.Background,QColor(75,75,75))
        self.setPalette(palette)
        # self.setAttribute(Qt.WA_TranslucentBackground)
        #全局属性
        self.user_name=username #默认用户名
        self.dir=os.path.dirname(os.path.dirname(__file__))
        # self.PROJECT_INFO_PATH=os.path.join(self.dir,'projects')#这里要改为共享盘符
        self.PROJECT_INFO_PATH = project_info_path #项目信息路径
        self.IMG_PATH=os.path.join(os.path.dirname(os.path.dirname(__file__)),'imgs')
        self.CONFIG_PATH=os.path.join(os.path.dirname(os.path.dirname(__file__)),'config')

        #预设内容
        self.project_list=[] #项目列表
        self.setProjectList() #读取本地项目数据中的项目列表


        #缓存属性
        self.current_project_name='' #当前项目名称
        self.current_project_path=''#当前项目数据库路径
        self.current_real_project_path=''#当前项目真实路径

        self.current_department_name=''#当前部门名称
        self.current_real_department_path=''#当前真实部门绝对路径
        self.current_real_epi_name=[]# ''#当前真实集数名称
        self. current_real_epi_path=[] #''#当前真实集数绝对路径
        self.current_real_shot_name=''#当前选中的镜头名称
        self.current_real_shot_path=''#当前真实镜头绝对路径
        # self.setWindowTitle('BrickMover')
        # self.setStyleSheet('background-color:rgb(150,150,150)')
        icon=QIcon()
        icon_path=os.path.join(self.IMG_PATH,'zt.png')
        icon.addPixmap(QPixmap(icon_path))
        self.setWindowIcon(icon)
        self.main_widget=QWidget()
        self.main_widget_layout=QHBoxLayout()

        self.hSplitter=QSplitter(Qt.Horizontal)


        self.shot_widget=QWidget()#为了将shot整个布局加入Splitter,但Splitter只能加QWidget，所以先弄一个
        self.shot_layout=QVBoxLayout()

        btnStyle='QPushButton{background-color:rgb(100,100,100);color:rgb(200,200,200);border-radius:4px;padding:2px}QPushButton:hover{color:#EEE8AA}'
        self.shot_btn_layout=QHBoxLayout()
        self.shot_add_btn= QPushButton('添加镜头')
        self.shot_add_btn.setStyleSheet(btnStyle)
        self.shot_add_btn.clicked.connect(self.create_shot)
        self.shot_delete_btn=QPushButton('删除镜头')
        self.shot_delete_btn.setStyleSheet(btnStyle)
        self.shot_delete_btn.clicked.connect(self.delete_shot)
        self.shot_open_btn=QPushButton('打开镜头')
        self.shot_open_btn.setStyleSheet(btnStyle)
        self.shot_open_btn.clicked.connect(self.__openShot)
        self.shot_open_path_btn=QPushButton('打开目录')
        self.shot_open_path_btn.setStyleSheet(btnStyle)
        self.shot_open_path_btn.clicked.connect(self.__openShotDir)
        self.search_btn=QPushButton('搜索')
        self.search_btn.setStyleSheet(btnStyle)
        self.search_btn.clicked.connect(self.__openSearchWindow)
        self.shot_btn_layout.addWidget(self.shot_add_btn)
        self.shot_btn_layout.addWidget(self.shot_delete_btn)
        self.shot_btn_layout.addWidget(self.shot_open_btn)
        self.shot_btn_layout.addWidget(self.shot_open_path_btn)
        self.shot_btn_layout.addWidget(self.search_btn)


        #表格
        self.shot_table_widget=MainTableWidget()
        self.shot_table_widget.shot_name_Signal.connect(self.__setCurrentShotName)
        self.shot_table_widget.count_price_Signal.connect(self.__refreshCountPrice)#刷新数量与价格统计

        #统计
        self.count_layout=QHBoxLayout()
        self.count_label=QLabel('数量:')
        self.count_label.setStyleSheet('color:rgb(200,200,200)')
        self.count=QLabel('0')
        self.count.setStyleSheet('color:rgb(200,200,200)')
        self.price_sum_label=QLabel('总价:')
        self.price_sum_label.setStyleSheet('color:rgb(200,200,200)')
        self.price_sum=QLabel('0')
        self.price_sum.setStyleSheet('color:rgb(200,200,200)')
        self.count_layout.addWidget(self.count_label)
        self.count_layout.addWidget(self.count)
        self.count_layout.addWidget(self.price_sum_label)
        self.count_layout.addWidget(self.price_sum)


        self.shot_layout.addLayout(self.shot_btn_layout)
        self.shot_layout.addWidget(self.shot_table_widget)
        self.shot_layout.addLayout(self.count_layout)
        self.shot_widget.setLayout(self.shot_layout)


        #百度云
        self.yunpan=YunPan()
        #
        # #意见反馈
        # self.feedback_window=Feedback()

        self.hSplitter.addWidget(self.shot_widget)
        self.hSplitter.addWidget(self.yunpan)
        self.hSplitter.setStretchFactor(0,8)
        self.hSplitter.setStretchFactor(1,2)

        self.main_widget_layout.addWidget(self.hSplitter)

        self.main_widget.setLayout(self.main_widget_layout)

        self.setCentralWidget(self.main_widget)

        #创建菜单栏
        self.createMenus()

        #创建工具栏
        self.createToolBar()

        #创建Dock
        self.createDocks()



    def createMenus(self):

        self.menuBar().setStyleSheet("background-color:rgb(75,75,75);color:rgb(200,200,200);border: 0px")#;padding: -1; border: 0
        # self.menuBar().setStyleSheet("padding: -1; border: 0")
        self.projectMenu=self.menuBar().addMenu("&项目")


        self.create_project_Action=QAction(u'创建项目',self)
        self.create_project_Action.triggered.connect(self.createProject)
        self.projectMenu.addAction(self.create_project_Action)
        self.create_project_Action=QAction(u'打开项目',self)
        self.create_project_Action.triggered.connect(self.openProject)
        self.projectMenu.addAction(self.create_project_Action)
        self.delete_project_Action=QAction(u'删除项目信息',self)
        self.delete_project_Action.triggered.connect(self.delProject)
        self.projectMenu.addAction(self.delete_project_Action)

        self.rename_project_Action=QAction(u'修改项目名称')
        self.rename_project_Action.triggered.connect(self.openRenameWindow)
        self.projectMenu.addAction(self.rename_project_Action)

        self.set_projectPath_Action=QAction(u'同步项目')
        self.set_projectPath_Action.triggered.connect(self.setProjectPath)
        # self.projectMenu.addAction(self.set_projectPath_Action)



        self.departmentMenu=self.menuBar().addMenu("&部门")
        self.addDepartAction=QAction(u'创建部门')
        self.addDepartAction.triggered.connect(self.createDepartment)
        self.departmentMenu.addAction(self.addDepartAction)


        self.viewMenu=self.menuBar().addMenu("&窗口")

        self.settingMenu=self.menuBar().addMenu('设置')
        self.cancel_autocheck_Action=QAction('取消自动登录')
        self.cancel_autocheck_Action.triggered.connect(self.cancel_autocheck)
        self.settingMenu.addAction(self.cancel_autocheck_Action)
        self.helpMenu=self.menuBar().addMenu("&帮助")
        self.docAction=QAction(u'帮助文档')
        self.creatorAction=QAction(u'制作者信息')
        self.versionAction=QAction(u'版本信息')
        self.helpMenu.addAction(self.docAction)
        self.helpMenu.addAction(self.creatorAction)
        self.helpMenu.addAction(self.versionAction)

    def createToolBar(self):
        self.leftToolBar=self.addToolBar('tool1')
        self.titleFront=QFont()
        self.titleFront.setPointSize(7)

        self.fontsize=QFont()
        self.fontsize.setPointSize(7)

        self.left_widget=QWidget()
        self.left_layout=QHBoxLayout()
        self.left_layout.setContentsMargins(2,0,2,0)

        self.left_btn1=QToolButton(self)
        self.left_btn1.setIcon(QIcon(os.path.dirname(__file__)+'/imgs/blank.png'))
        self.left_btn1.clicked.connect(self.__batchCreate)
        self.left_btn2=QToolButton(self)
        self.left_btn2.setIcon(QIcon(os.path.dirname(__file__)+'/imgs/blank.png'))
        self.left_layout.addWidget(self.left_btn1)
        self.left_layout.addWidget(self.left_btn2)
        self.left_widget.setLayout(self.left_layout)

        self.leftToolBar.addWidget(self.left_widget)



    def createDocks(self):
        #项目窗口
        dockStyle='QWidget{background-color:rgb(75,75,75);color:rgb(200,200,200);border: 0px}QScrollBar{background-color:rgb(100,100,100);width:10px}'

        self.left_dock=QDockWidget(self)
        self.left_dock.setWindowTitle('项目列表')
        self.left_dock.setMaximumSize(250,550)
        self.left_dock.setStyleSheet(dockStyle)
        # self.left_dock.setMinimumWidth(500)
        # self.left_dock_widget=QWidget()
        # self.left_dock_widget.setStyleSheet('QWidget{background-color:rgb(75,75,75);color:rgb(200,200,200)}QScrollbar{background-color:rgb(100,0,0)}')

        # self.left_dock.setStyleSheet('QDockWidget{background-color:rgb(75,75,75);color:rgb(200,200,200)}')
        self.left_dock_layout=QVBoxLayout()
        self.left_dock_layout.setAlignment(Qt.AlignTop)


        self.project_radio_widget=RadioWidget(parent=self)
        # self.project_radio_widget.setStyleSheet('QScrollBar{background-color:rgb(100,0,0)}')
        self.project_radio_widget.projectName_projectPath_Signal.connect(self.setCurrentProjectName) #获取所选中的项目名称，用于获取其真实项目路径
        self.project_radio_widget.setRadioBtnList(self.project_list)

        # self.left_dock_layout.addWidget( self.project_radio_widget)
        # self.left_dock_widget.setLayout(self.left_dock_layout)

        self.left_dock.setWidget(self.project_radio_widget)
        self.addDockWidget(Qt.LeftDockWidgetArea,self.left_dock)
        self.viewMenu.addAction(self.left_dock.toggleViewAction())


        #部门阶段
        self.left_mid_dock=QDockWidget()
        self.left_mid_dock.setWindowTitle('部门阶段')
        self.left_mid_dock.setMaximumSize(250,550)
        self.left_mid_dock.setStyleSheet(dockStyle)

        self.department_radio_widget=DepartmentWidget()
        self.department_radio_widget.departmentPath_epiList_Signal.connect(self.setCurrentDepartmentName) #获取所选中的部门阶段名称，用于获取其真实部门路径
        self.department_radio_widget.departmentPath_epiList_epiName_Signal.connect(self.__resetCurrentDepartmentName)#搜索后激活的信息重新刷新部门与集数，同时指定集数
        self.department_radio_widget.setRadioBtnList(['<空>'])
        # self.department_radio_widget.setC(['<空>'])

        self.left_mid_dock.setWidget(self.department_radio_widget)

        self.addDockWidget(Qt.LeftDockWidgetArea,self.left_mid_dock)
        self.viewMenu.addAction(self.left_mid_dock.toggleViewAction())



        #集数/场数
        self.left_buttom_dock=QDockWidget()
        self.left_buttom_dock.setWindowTitle('集数/场数')
        self.left_buttom_dock.setStyleSheet(dockStyle)
        # self.left_buttom_dock.setMinimumSize(250,150)

        self.epi_radio_widget=EpiWidget()
        # self.epi_radio_widget.setRadioBtnList(['<空>'])
        self.epi_radio_widget.setCheckboxList(['<空>'])
        self.epi_radio_widget.epiName_epiPath_shotList_Signal.connect(self.refreshShotWindow)#点击集数就可以开始刷新镜头了
        self.epi_radio_widget.epiName_epiPath_shotList_shot_Signal.connect(self.__refreshShotWindow)#搜索后返回
        self.left_buttom_dock.setWidget(self.epi_radio_widget)
        self.addDockWidget(Qt.LeftDockWidgetArea,self.left_buttom_dock)
        self.viewMenu.addAction(self.left_buttom_dock.toggleViewAction())





        #提交窗口与小样资产窗口
        self.submit_dock=QDockWidget()
        self.submit_dock.setWindowTitle('提交小样')
        self.submit_dock.setStyleSheet(dockStyle)
        self.submit_list_widget=FileListWidget()

        self.submit_dock.setWidget(self.submit_list_widget)
        self.addDockWidget(Qt.BottomDockWidgetArea,self.submit_dock)
        self.viewMenu.addAction(self.submit_dock.toggleViewAction())


        self.project_dock=QDockWidget()
        self.project_dock.setWindowTitle('提交工程')
        self.project_dock.setStyleSheet(dockStyle)
        self.project_submit_widget=ProjectListWidget()
        self.project_dock.setWidget(self.project_submit_widget)
        self.addDockWidget(Qt.BottomDockWidgetArea,self.project_dock)
        self.viewMenu.addAction(self.project_dock.toggleViewAction())

        self.time_dock = QDockWidget()
        self.time_dock.setWindowTitle('时间栏')
        self.time_dock.setStyleSheet(dockStyle)
        self.time_info_window=TimeInfo()
        self.time_info_window.send_date_Signal.connect(self.__refreshStartDay)
        self.time_dock.setWidget(self.time_info_window)
        self.addDockWidget(Qt.BottomDockWidgetArea, self.time_dock)
        self.viewMenu.addAction(self.time_dock.toggleViewAction())

        ##返修意见以及镜头信息
        self.retake_dock=QDockWidget()
        self.retake_dock.setWindowTitle('意见反馈')
        self.retake_dock.setStyleSheet(dockStyle)
        self.opinion_window=Opinion(self.user_name)
        self.retake_dock.setWidget(self.opinion_window)
        self.addDockWidget(Qt.RightDockWidgetArea,self.retake_dock)
        self.viewMenu.addAction(self.retake_dock.toggleViewAction())

        # #百度云
        # self.yunpan_dock=QDockWidget()
        # self.yunpan_dock.setWindowTitle('百度云盘')
        #
        # self.yunpan=YunPan()
        # self.yunpan_dock.setWidget(self.yunpan)
        # self.addDockWidget(Qt.RightDockWidgetArea,self.yunpan_dock)
        # self.viewMenu.addAction(self.yunpan_dock.toggleViewAction())


        self.shotInfo_dock=QDockWidget()
        self.shotInfo_dock.setWindowTitle('镜头信息')
        self.shotInfo_dock.setStyleSheet(dockStyle)
        self.shotInfo_window=ShotInfo()
        self.shotInfo_dock.setWidget(self.shotInfo_window)
        self.addDockWidget(Qt.RightDockWidgetArea,self.shotInfo_dock)
        self.viewMenu.addAction(self.shotInfo_dock.toggleViewAction())





        #镜头列表
#工具架
    def __batchCreate(self):
        '''
        该函数用来快速批量创建部门与集数，避免多次重复创建
        :return:
        '''
        pass


#操作函数

    #项目窗口
    def createProject(self):
        '''
        弹出创建项目窗口
        :return:
        '''
        self.cp=CreateProject(self.PROJECT_INFO_PATH)
        self.cp.new_project_Signal.connect(self.addProject)
        self.cp.show()

        # QMessageBox.information(self,'create project','这里跳转窗口项目面板')

    def openProject(self):
        '''
        在projects中读取所有存在的项目info
        :return:
        '''
        project_list=os.listdir(self.PROJECT_INFO_PATH)
        if project_list is []:
            QMessageBox.information(self,'提示','项目为空，请先创建项目')
        else:
            self.left_dock.move(500,200)
            self.left_dock.setFloating(True)
            # QMessageBox.information(self,'w','这里让项目列表显示出来')


    def delProject(self):
        '''
        该函数用来删除项目列表当前选中的项目
        :return:
        '''
        self.project_radio_widget.deleteCurrentRadioBtn()

    def setProjectPath(self):
        """
        同步项目信息，将远程项目信息复制到本地，意味着每次都要同步信息，如果有数据库可以直接调用
        :return:
        """
        self.setProjectWindow=SetProjectPath()
        self.setProjectWindow.project_db_path_Signal.connect(self.__setProjectDB_path)
        self.setProjectWindow.show()

    def __setProjectDB_path(self,path):
        if not os.path.exists(self.PROJECT_INFO_PATH):
            shutil.copytree(path, self.PROJECT_INFO_PATH)
        else:
            dir=os.listdir(path)
            # print(dir)
            if dir:
                for i in os.listdir(self.PROJECT_INFO_PATH):
                    filepath=os.path.join(self.PROJECT_INFO_PATH,i)
                    os.remove(filepath)

            for g in dir:
                origin_file_path=os.path.join(path,g)
                new_file_path=os.path.join(self.PROJECT_INFO_PATH,g)
                shutil.copyfile(origin_file_path,new_file_path)

            # try:
            #     dir=os.path.dirname(path)
            #     if dir:
            #         for i in os.listdir(self.PROJECT_INFO_PATH):
            #             filepath=os.path.join(self.PROJECT_INFO_PATH,i)
            #             os.remove(filepath)
            #
            #     for g in dir:
            #         origin_file_path=os.path.join(dir,g)
            #         new_file_path=os.path.join(self.PROJECT_INFO_PATH,g)
            #         shutil.copyfile(origin_file_path,new_file_path)
            #     # shutil.rmtree(self.PROJECT_INFO_PATH)
            #     # shutil.copytree(path, self.PROJECT_INFO_PATH)
            # except:
            #     QMessageBox.information(self,'错误','同步项目失败，请检查路径是否正确！')
        self.project_list=os.listdir(self.PROJECT_INFO_PATH)
        self.project_radio_widget.setRadioBtnList(self.project_list)


    def openRenameWindow(self):
        self.rename = Rename('狮子王')
        self.rename.show()
        self.rename.newName_Signal.connect(self.renameProject)


    def renameProject(self,newname):
        '''
        改名槽函数
        :param newname:
        :return:
        '''
        QMessageBox.information(self,'改名','这里更改所选中的项目的名称')


    def addProject(self,name):
        '''
        该函数为槽函数，由创建项目窗口传的信号过来，内容为新项目名称
        :param name:
        :return:
        '''
        self.project_list.append(name)
        self.project_radio_widget.setRadioBtnList(self.project_list)


      #部门窗口      #
    def createDepartment(self):
        '''
        该函数实现在当前选中的项目中创建新的部门，部门操作都是直接真实项目路径下操作的
        :return:
        '''
        if self.current_project_name:
            self.createDepartmentWindow=CreateDepartment(self.current_project_name)
            self.createDepartmentWindow.department_Signal.connect(self.setDepartment)
            self.createDepartmentWindow.show()

        else:
            QMessageBox.information(self,'提示','请先选择一个具体项目')

    #取消自动登录
    def cancel_autocheck(self):
        custom_cfg=os.path.join(self.CONFIG_PATH,'custom.info')
        if custom_cfg:
            custom_info_str=''
            with open(custom_cfg,'r',encoding='utf8') as r:
                custom_info_str=r.read()
            if custom_info_str:
                custom_info=eval(custom_info_str)
                custom_info['autocheck']=0
                with open(custom_cfg,'w',encoding='utf8') as w:
                    w.write(str(custom_info))
                QMessageBox.information(None,'提示','已取消自动登录')


    def getDepartmentList(self,real_project_path):
        '''
        获取选中的真实项目路径下的所有部门列表
        :param real_project_path:
        :return:
        '''
        depart_list=os.listdir(self.current_real_project_path)
        return depart_list


    def setCurrentDepartmentName(self,department_tuple):
        '''
        槽函数获取从部门列表中选中的部门名称，存给全局当前选择部门，刷新shot_table
        :param department_tuple:
        :return:
        '''
        # print("有没有")
        print(department_tuple)
        self.current_department_name=department_tuple[0]
        self.current_real_department_path=department_tuple[1]
        # self.epi_radio_widget.setRadioBtnList(department_tuple[2])
        self.epi_radio_widget.setCheckboxList(department_tuple[2])
        self.epi_radio_widget.setCurrentProjectName(self.current_project_name)
        self.epi_radio_widget.setCurrentDepartment(self.current_department_name)

        # print(self.current_department_name)
        self.epi_radio_widget.setRealDepartmentPath(self.current_real_department_path)

        #清空镜头栏
        self.shot_table_widget.verticalClear()


    def __resetCurrentDepartmentName(self,department_tuple):
        '''
        槽函数从部门窗口传来的信息,是搜索后的信号，刷新集数信息
        :param department_tuple: (department,department_path,epiList,chooseEpi)
        :return:
        '''
        self.current_department_name=department_tuple[0]
        self.current_real_department_path=department_tuple[1]
        # self.epi_radio_widget.setRadioBtnList(department_tuple[2])
        self.epi_radio_widget.setCheckboxList(department_tuple[2])
        self.epi_radio_widget.setCurrentProjectName(self.current_project_name)
        self.epi_radio_widget.setCurrentDepartment(self.current_department_name)
        self.epi_radio_widget.setRealDepartmentPath(self.current_real_department_path)


    #集/场窗口




    #镜头窗口
    def create_shot(self):
        '''
        创建新镜头
        :return:
        '''
        # if self.current_real_epi_path !='':
        if len(self.current_real_epi_path)==1:
            self.create_type_window=CreateShotTypeChoose()
            self.create_type_window.create_type_Signal.connect(self._create_shot)
            self.create_type_window.show()

            self.cs=CreateShot(self.current_real_epi_path[0])
            self.cs.all_shot_info_Signal.connect(self.refreshShotWindow)
            self.cs.show()
        else:
            QMessageBox.information(self,'注意','请先选择单个集数进行添加镜头')

    def _create_shot(self,create_type):
        if create_type==0:
            print('文本创建')
        elif create_type==1:
            if len(self.current_real_epi_path) == 1:
                self.cs = CreateShot(self.current_real_epi_path[0])
                self.cs.all_shot_info_Signal.connect(self.refreshShotWindow)
                self.cs.show()
            else:
                QMessageBox.information(self, '注意', '请先选择单个集数进行添加镜头')

    def delete_shot(self):
        '''
        删除当前选中的镜头
        :return:
        '''
        self.dw=DeleteWarn(3)
        self.dw.out_Signal.connect(self.__real_delete)
        self.dw.show()


    def __real_delete(self,back):
        if back:
            if self.current_real_shot_name != '':
                self.current_real_shot_path = os.path.join(self.current_real_epi_path, self.current_real_shot_name)
                try:
                    shutil.rmtree(self.current_real_shot_path)
                    out_tuple = self.__getCurrentInfo_tuple()
                    self.refreshShotWindow(out_tuple)
                except:
                    QMessageBox.information(self, '提示', '当前目录下有文件正在使用')

            else:
                QMessageBox.information(self, '提示', '请先选择镜头')
        else:
            pass

    def __openShot(self):
        '''
        打开当前原镜头
        :return:
        '''
        try:
            video_path=''
            shotdir_list=os.listdir(self.current_real_shot_path)
            for i in shotdir_list:
                if i.startswith(self.current_real_shot_name):
                    video_path=os.path.join(self.current_real_shot_path,i)
                    break
            if video_path=='':
                QMessageBox.information(self, '提示', '没有找到该镜头视频')
            else:
                os.startfile(video_path)
        except:
            QMessageBox.information(self,'提示','打开失败,请选择镜头视频并确保该视频镜头真实存在')

    def __openShotDir(self):
        '''
        打开当前镜头目录
        :return:
        '''
        if self.current_real_epi_path =='':
            QMessageBox.information(self,'提示','请先选择具体集数')
        else:
            os.startfile(self.current_real_epi_path)

    def __openSearchWindow(self):
        if not self.current_real_project_path:
            QMessageBox.information(self,'提示','请先选择查询项目')
        else:
            self.search_window=Search(self.current_real_project_path)
            self.search_window.shot_path_Signal.connect(self.__searchForDepartment)
            self.search_window.show()

    def __searchForDepartment(self,temp_tuple):
        '''
        槽函数，从搜索结果中获取到的信号,操作部门窗口
        :param temp_tuple:本集搜索:(0,shotname)，全局搜索:(1,shotname,real_shot_path)
        :return:
        '''
        if temp_tuple[0]==0:#本集搜索
            index=0
            rowCount=self.shot_table_widget.rowCount()
            for i in range(rowCount):
                item= self.shot_table_widget.verticalHeaderItem(i)
                if item:
                    text=item.text()
                    if text==temp_tuple[1]:
                        index=i
            self.shot_table_widget.verticalScrollBar().setValue(index)



        else:#全局搜索
            self.current_real_shot_name=temp_tuple[1]
            self.current_real_shot_path=temp_tuple[2]
            self.current_real_epi_path=os.path.dirname(self.current_real_shot_path)
            self.current_real_epi_name=os.path.split(self.current_real_epi_path)[1]
            self.current_real_department_path=os.path.dirname(self.current_real_epi_path)
            self.current_department_name=os.path.split(self.current_real_department_path)[1]
            self.department_radio_widget.setCurrentRadioBtn(self.current_real_department_path, self.current_department_name)
            self.epi_radio_widget.setCurrentRadioBtn(self.current_real_epi_path,self.current_real_epi_name,self.current_real_shot_name)


    def __getCurrentInfo_tuple(self):
        '''
        计算函数，用来计算表格窗口刷新时所需要的所有镜头信息
        :return:
        '''
        shot_list=os.listdir(self.current_real_epi_path)

        if shot_list !=[]:
            shot_info_list=[]
            for i in shot_list:
                shot_info_dict={}
                shot_dir_path=os.path.join(self.current_real_epi_path,i)
                shot_info_path=os.path.join(shot_dir_path,'info')
                shot_info=self.__readInfo(shot_info_path)
                shot_info_dict[i]=shot_info
                shot_info_list.append(shot_info_dict)

            out_tuple=(self.current_real_epi_path,shot_list,shot_info_list)
            return out_tuple
        else:
            out_tuple = (self.current_real_epi_path,shot_list,[])
            return out_tuple

    def __readInfo(self,info_path):

        info=''
        with open(info_path,'r',encoding='utf-8') as r:
            read=r.read()
            info=eval(read)

        return info


    def __setCurrentShotName(self,shot_tuple):
        '''
        槽函数，接受点击具体镜头号传来的信息,这里要传给任务信息栏、返修意见、提交镜头小样、提交工程
        :param shot_tuple: 包含current_real_shot_path,与shot_name ,start_day,given_time,注：这里的shot都是指整个镜头文件目录
        :return:
        '''
        self.current_real_shot_path=shot_tuple[0]
        self.current_real_shot_name=shot_tuple[1]#这个给镜头信息的

        #刷新小样栏
        self.submit_list_widget.setCurrentShotPath(self.current_real_shot_path)
        sample_list=os.listdir(os.path.join(self.current_real_shot_path,'sample'))
        self.submit_list_widget.setList(sample_list)


        #刷新提交工程栏
        self.project_submit_widget.setCurrentShotPath(self.current_real_shot_path)
        project_list=os.listdir(os.path.join(self.current_real_shot_path,'project'))
        self.project_submit_widget.setList(project_list)


        #刷新返修意见栏
        self.opinion_window.setCurrentOpinionPath(self.current_real_shot_path)

        self.opinion_window.showList()

        #镜头信息栏
        self.shotInfo_window.setCurrentReferencePath(self.current_real_shot_path,self.current_real_shot_name)
        self.shotInfo_window.showInfo()
        #时间栏
        # print(shot_tuple[2],shot_tuple[3])
        # print(shot_tuple[3]-1)
        self.time_info_window.setLeftDay(shot_tuple[2],shot_tuple[3])


    #时间窗口
    def __refreshStartDay(self,date):
        '''
        槽函数，接受从时间栏中重新选择起始日期的信号
        :param date:
        :return:
        '''
        shot_info_path=os.path.join(self.current_real_shot_path,'info')
        info={}
        with open(shot_info_path,'r',encoding='utf-8') as r:
            read=r.read()
            info=eval(read)

        info['start_day']=date

        with open(shot_info_path,'w',encoding='utf-8') as w:
            w.write(str(info))




    #设置内容函数
    def setUserName(self,username):
        '''
        设置当前使用者
        :param username:
        :return:
        '''
        self.user_name=username

    def setDepartment(self,department_list):
        '''
        槽函数，直接设置部门阶段内容的
        :param department_list:传给部门窗口
        :return:
        '''

        self.department_radio_widget.setRadioBtnList(department_list)



    def setProjectList(self):
        self.project_list=os.listdir(self.PROJECT_INFO_PATH)

    def setCurrentProjectName(self,projectName_projectPath_tuple):
        '''
        槽函数，接受从项目列表中传来的，当前所选中的项目,
        注意，既然要实现点击项目列表然后刷新部门列表，那么该槽函数还必须得刷新部门函数
        :param projectName_projectPath_tuple:内含，当前选中的项目，真实项目路径，以及当前项目下的部门列表
        :return:
        '''
        if projectName_projectPath_tuple != ():
            self.current_project_name=projectName_projectPath_tuple[0]
            self.current_real_project_path=projectName_projectPath_tuple[1]#self.getCurrentProjectPath(self.current_project_name)
            # print(projectName_projectPath_tuple[1])

            self.department_radio_widget.setRadioBtnList(projectName_projectPath_tuple[2])
            self.department_radio_widget.setCurrentProjectName(self.current_project_name)
            self.department_radio_widget.setRealProjectPath(self.current_real_project_path)  #给department_widget免得再计算真实项目路径
            # self.department_radio_widget.setRadioBtnList(['动画','跟踪','渲染','特效','美术','合成'])

            #清空集数内容
            # self.epi_radio_widget.setRadioBtnList([])
            self.epi_radio_widget.setCheckboxList([])
            #清空镜头
            self.shot_table_widget.verticalClear()

        else:
            # 清空部门内容
            self.department_radio_widget.setRadioBtnList([])
            # 清空集数内容
            # self.epi_radio_widget.setRadioBtnList([])
            self.epi_radio_widget.setCheckboxList([])
            # 清空镜头
            self.shot_table_widget.verticalClear()
    def getCurrentProjectPath(self,projectName):
        '''
        计算函数，通过给定的项目名称，访问本地项目数据库projects中该项目内的project_path真实项目路径
        :param projectName:
        :return:
        '''
        project_path=self.PROJECT_INFO_PATH+'/'+projectName
        if not os.path.exists(project_path):
            QMessageBox.information(self,'提示','没有关联该项目')
            return False
        else:
            real_project_path=''
            with open(project_path,'r',encoding='utf-8') as r:
                read=r.read()
                projectInfo=eval(read)
                real_project_path=projectInfo['project_path']

            self.current_project_path=real_project_path

            return real_project_path


    def refreshShotWindow(self,epi_tuple):
        '''
        该函数把所有当前目录下的镜头信息传给镜头表格，内容包括给垂直表头的 镜头名称，以及里面每个
        镜头里的info信息
        :param epi_tuple:(self.current_real_epi_path, shot_list, shot_info_list)
        :return:
        '''
        self.current_real_epi_path=epi_tuple[0]
        shot_name_list=epi_tuple[1]
        shot_info_list=epi_tuple[2]#这个是镜头info列表

        # self.shot_table_widget.updateRowCount(len(shot_info_list))
        # self.shot_table_widget.setV_list(shot_name_list)
        # self.shot_table_widget.show_all_info(epi_tuple)
        #
        # count_total_price_info = self.shot_table_widget.count_total_price_Info()
        # self.__refreshCountPrice(count_total_price_info)

    def __refreshShotWindow(self,epi_tuple):
        '''
        槽函数，从搜索后集数发来的信号，带垂直镜头列表，每个镜头里的info信息,还有当前选中的镜头
        :param epi_tuple:(self.current_real_epi_path, shot_list, shot_info_list,current_shot_name)
        :return:
        '''

        self.current_real_epi_path=epi_tuple[0]
        self.current_real_shot_name=epi_tuple[3]

        shot_name_list=epi_tuple[1]
        shot_info_list=epi_tuple[2]#这个是镜头info列表
        index=shot_name_list.index(self.current_real_shot_name)

        self.shot_table_widget.updateRowCount(len(shot_info_list))
        self.shot_table_widget.setV_list(shot_name_list)
        self.shot_table_widget.show_all_info(epi_tuple)
        self.shot_table_widget.verticalScrollBar().setValue(index)

        count_total_price_info=self.shot_table_widget.count_total_price_Info()
        self.__refreshCountPrice(count_total_price_info)

    def __refreshCountPrice(self,temp_tuple):
        '''
        刷新当前数量与总价
        :param temp_tuple:
        :return:
        '''
        print(temp_tuple)
        self.count.setText(str(temp_tuple[0]))
        self.price_sum.setText(str(temp_tuple[1]))

    def keyPressEvent(self, event):
        if event.key()==Qt.Key_F4:
            print('PROJECT_INFO_PATH:',self.PROJECT_INFO_PATH)


if __name__ == '__main__':
    app=QApplication(sys.argv)
    mainWindow=MainWindow('老张')
    mainWindow.show()
    sys.exit(app.exec_())

